package io.github.consoles.dsa;

import java.util.Iterator;

/**
 * Created by yiihua-013 on 17/2/10.
 * <p>
 * 双向链表
 */
public class DoublyLinkedList<Item> implements Iterable<Item> {

    private int              size;
    private DoubleNode<Item> first;
    private DoubleNode<Item> last;

    /**
     * 表头插入节点
     *
     * @param node
     */
    public void insertFirst(DoubleNode node) {
        if (isEmpty()) {
            last = node;
        } else {
            node.next = first;
            first.prev = node;
        }
        first = node;
        size++;
    }

    public boolean isEmpty() {
        return first == null;
    }

    /**
     * 表尾插入
     *
     * @param node
     */
    public void append(DoubleNode node) {
        if (isEmpty()) {
            first = node;
            last = node;
        } else {
            node.prev = last;
            last.next = node;
            last = node;
        }
        size++;
    }

    /**
     * 删除表头
     *
     * @return
     */
    public DoubleNode deleteFirst() {
        if (isEmpty()) return null;

        DoubleNode deletedFirst = first;

        if (first.next != null) first.next.prev = null;
        first = first.next;

        if (first == null) last = null;
        size--;

        return deletedFirst;
    }

    /**
     * 删除表尾
     */
    public DoubleNode deleteLast() {
        if (isEmpty()) return null;

        DoubleNode deletedLast = last;

        if (last == null) return null;

        if (first == last) {
            first = null;
        } else {
            last.prev.next = null;
        }
        last = last.prev;
        size--;

        return deletedLast;
    }

    /**
     * 在base节点之前插入node节点
     *
     * @param base
     * @param node
     */
    public void insertBefore(DoubleNode base, DoubleNode node) {

        if (node == null) return;

        if (base == first) insertFirst(node);
        else {
            base.prev.next = node;
            node.prev = base.prev;
            node.next = base;
            base.prev = node;
            size++;
        }
    }

    public void insertAfter(DoubleNode base, DoubleNode node) {
        if (node == null) return;

        if (base == last) {
            last.next = node;
            node.prev = last;
            last = node;
        } else {
            node.next = base.next;
            node.prev = base;
            base.next.prev = node;
            base.next = node;
        }
        size++;
    }

    /**
     * 删除指定节点
     *
     * @param node
     */
    public void delete(DoubleNode node) {

        DoubleNode current = first;
        while (current != last) {
            if (current == node) {
                if (current == first) {
                    deleteFirst();
                } else if (current == last) {
                    deleteLast();
                } else {
                    current.prev.next = current.next;
                    current.next.prev = current.prev;
                    size--;
                }
                break;
            }
            current = current.next;
        }
    }

    private class DoubleLinkedIterator implements Iterator<Item> {

        private DoubleNode current = first;

        DoubleLinkedIterator(){
            System.out.println("");
        }

        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public Item next() {
            Item item = (Item) current.item;
            current = current.next;
            return  item;
        }
    }

    @Override
    public Iterator<Item> iterator() {
        return new DoubleLinkedIterator();
    }
}
